package com.sh3h.generator.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import com.sh3h.generator.web.domain.GenDataSource;
import com.sh3h.generator.web.domain.GenFieldType;
import com.sh3h.generator.web.domain.GenTable;
import com.sh3h.generator.web.domain.GenTableField;
import com.sh3h.generator.web.domain.query.entity.TableFieldQuery;
import com.sh3h.generator.web.domain.query.entity.TableQuery;
import com.sh3h.generator.web.entity.TableEntity;
import com.sh3h.generator.web.entity.TableFieldEntity;
import com.sh3h.generator.web.service.GenTableService;
import com.sh3h.generator.web.mapper.GenTableMapper;
import com.sh3h.generator.web.utils.GenUtils;
import com.sh3h.generator.web.utils.StrUtils;
import org.apache.commons.text.CaseUtils;
import org.apache.ibatis.jdbc.RuntimeSqlException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.rmi.ServerException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 3HWX-YY-BJB-11
 * @description 针对表【gen_table(代码生成表)】的数据库操作Service实现
 * @createDate 2024-01-05 17:17:27
 */
@Service
public class GenTableServiceImpl extends ServiceImpl<GenTableMapper, GenTable>
        implements GenTableService {
    @Autowired
    private GenDatasourceServiceImpl datasourceService;
    @Autowired
    private GenFieldTypeServiceImpl fieldTypeService;
    @Autowired
    private GenTableServiceImpl tableService;
    @Autowired
    private GenTableFieldServiceImpl tableFieldService;

    @Override
    public void tableImport(String datasourceId, String tableId, String tableName) throws ServerException, SQLException, ClassNotFoundException {
        GenDataSource dataSource = datasourceService.getById(datasourceId);
        List<GenFieldType> genFieldTypeList = fieldTypeService.list();
        //查询表是否存在
        LambdaQueryWrapper<GenTable> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(GenTable::getTableName, tableName);
        GenTable genTable = tableService.getOne(queryWrapper);
        if (genTable != null) {
            throw new ServerException(tableName + "已存在");
        }
        //获取db中的表
        genTable = new GenTable();
        TableQuery tableQuery = new TableQuery(dataSource);
        TableEntity tableEntity = GenUtils.getTable(tableQuery, tableName);
        genTable.setTableName(tableEntity.getTableName());
        genTable.setTableComment(tableEntity.getTableComment());
        genTable.setClassName(StrUtils.underlineToFirstUpperCamel(tableEntity.getTableName()));
        //添加表
        tableService.save(genTable);
        genTable = tableService.getOne(queryWrapper);
        //获取db中的最新的表字段信息
        List<TableFieldEntity> tableFieldEntities = GenUtils.getTableFieldList(tableQuery, genTable.getId(), tableName);
        for (TableFieldEntity entity :
                tableFieldEntities) {
            GenTableField genTableField = new GenTableField();
            GenFieldType _gft = genFieldTypeList.stream().filter(a -> a.getColumnType().equals(entity.getFieldType())).findFirst().get();
            if (_gft != null) {
                genTableField.setAttrType(_gft.getAttrType());
                genTableField.setPackageName(_gft.getPackageName());
            }
            genTableField.setAttrName(StrUtils.underlineToCamel(entity.getFieldName()));
            genTableField.setFieldName(entity.getFieldName());
            genTableField.setFieldType(entity.getFieldType());
            genTableField.setFieldComment(entity.getFieldComment());
            genTableField.setTableId(entity.getTableId());
            genTableField.setPrimaryPk(entity.getPrimaryPk());
            //保存表字段信息
            tableFieldService.save(genTableField);
        }
    }

    @Transactional
    @Override
    public void syncTable(String tableId) throws ServerException, SQLException, ClassNotFoundException {
        List<GenFieldType> genFieldTypeList = fieldTypeService.list();
        //查询表是否存在
        GenTable genTable = tableService.getById(tableId);
        if (genTable == null) {
            throw new ServerException(genTable.getTableName() + "不存在");
        }
        GenDataSource dataSource = datasourceService.getById(genTable.getDatasourceId());
        TableQuery tableQuery = new TableQuery(dataSource);
        //获取db中的表字段信息
        List<TableFieldEntity> tableFieldEntities = GenUtils.getTableFieldList(tableQuery, genTable.getId(), genTable.getTableName());
        for (TableFieldEntity entity : tableFieldEntities) {
            //查询表是否存在
            LambdaQueryWrapper<GenTableField> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(GenTableField::getFieldName, entity.getFieldName());
            GenTableField gtf = tableFieldService.getOne(queryWrapper);
            if (gtf == null) {
                tableFieldService.remove(queryWrapper);
            }
            GenTableField genTableField = new GenTableField();
            GenFieldType _gft = genFieldTypeList.stream().filter(a -> a.getColumnType().equals(entity.getFieldType())).findFirst().get();
            if (_gft != null) {
                genTableField.setAttrType(_gft.getAttrType());
                genTableField.setPackageName(_gft.getPackageName());
            }
            genTableField.setAttrName(StrUtils.underlineToCamel(entity.getFieldName()));
            genTableField.setFieldName(entity.getFieldName());
            genTableField.setFieldType(entity.getFieldType());
            genTableField.setFieldComment(entity.getFieldComment());
            genTableField.setTableId(entity.getTableId());
            genTableField.setPrimaryPk(entity.getPrimaryPk());
            //保存表字段信息
            tableFieldService.save(genTableField);
        }
    }
}




